!function() {
    "use strict";
    var t = function(t) {
        return "未填写" + t
    }
        , e = function() {
        if ($.urlParam("redirect") || $.urlParam("next"))
            window.location = decodeURIComponent($.urlParam("redirect") || $.urlParam("next"));
        else {
            if (location.pathname.indexOf("tencent") > 0)
                return location.href = "/tencent/";
            location.href = "/"
        }
    }
        , a = function(t, a) {
        $("#auth-container").replaceWith($("#wechat-bind-container")),
            $("#wechat-bind-container .ask-scan").text(a),
            $("#wechat-bind-container").css("display", "block");
        new QRCode(document.getElementById("wechatQRCode"),t);
        setInterval(function() {
            $.ajax({
                method: "GET",
                url: "https://cn.udacity.com/cn/users/wechat_status"
            }).done(function(t) {
                "linked" === t.status && e()
            })
        }, 1e3),
            $("#skip-scan a").click(function(t) {
                t.preventDefault(),
                    e()
            })
    };
    if (/^\/signup/.test(location.pathname)) {
        var r = $.urlParam("qrcode");
        if (r && r.length > 0)
            return a(decodeURIComponent(r), "用微信扫描完成注册")
    }
    var o = function(t, r, o, n, i) {
        $.ajax({
            method: "POST",
            url: "https://cn.udacity.com/cn/users/" + t,
            data: JSON.stringify(r),
            contentType: "application/json",
            dataType: "json",
            timeout: 2e4
        }).done(function(r) {
            if (Cookies.set("_jwt_token", r.jwt, {
                    expires: 30,
                    domain: "localhost:9999" === location.host ? "localhost" : "udacity.com"
                }),
                    Cookies.set("_jwt", r.jwt, {
                        expires: 30,
                        domain: "localhost:9999" === location.host ? "localhost" : "udacity.com"
                    }),
                    $.ajaxSetup({
                        headers: {
                            Authorization: "Bearer " + r.jwt
                        }
                    }),
                    t.includes("register")) {
                if ("/" === location.pathname)
                    return void (location.href = "/signup/?qrcode=" + encodeURIComponent(r.wechat_qr_code_url));
                a(r.wechat_qr_code_url, "用微信扫描完成注册")
            } else
                r.bound_wechat_account ? e() : a(r.wechat_qr_code_url, "用微信扫描完成登录");
            o()
        }).always(i).fail(function(t) {
            n(t)
        })
    };
    document.addEventListener("DOMContentLoaded", function(e) {
        $("[data-logo-with-" + $.urlParam("ref") + "]").length && ($("[data-logo]").addClass("hide"),
            $("[data-logo-with-" + $.urlParam("ref") + "]").removeClass("hide")),
        "didi" === $.urlParam("ref") && $(".form-description").remove(),
            $('#form-submit input[type="password"]').on("change keyup", function(t) {
                $(t.target).val().length < 8 ? $(this).removeClass("success").addClass("warning") : $(this).removeClass("warning").addClass("success")
            });
        var a = $('#form-submit input[type="email"]');
        a.on("change keyup", function(t) {
            var e = $(this).data("validate")
                , a = $(t.target).val();
            !window.isEmail(a) || e && !RegExp(e).test(a) ? $(this).removeClass("success").addClass("warning") : $(this).removeClass("warning").addClass("success")
        }),
            a.autocomplete({
                default: a.data("hint-default") || "qq.com",
                hints: a.data("hint-sources") || ["qq.com", "gmail.com", "163.com", "126.com"]
            }),
            $("#form-submit").on("submit", function(e) {
                e.preventDefault(),
                    $("#alert-error").text(""),
                    $("#alert-error").hide();
                var r = $(this)
                    , n = r.data("endpoint")
                    , i = a.data("validate")
                    , s = {};
                if ($.each(r.serializeArray(), function(t, e) {
                        s[e.name] = e.value
                    }),
                    "register" === n) {
                    if ("" === s.first_name)
                        return $("#alert-error").text(t("名字")),
                            void $("#alert-error").show();
                    if ("" === s.last_name)
                        return $("#alert-error").text(t("姓名")),
                            void $("#alert-error").show();
                    if (s.password.length < 8)
                        return $("#alert-error").text("密码长度不得小于8位"),
                            void $("#alert-error").show()
                }
                return "" === s.email ? ($("#alert-error").text(t("电子邮箱")),
                    void $("#alert-error").show()) : i && !RegExp(i).test(s.email) ? ($("#alert-error").text(a.data("hint-error-msg") || "仅接受腾讯 @tencent.com 企业邮箱"),
                    void $("#alert-error").show()) : "" === s.password ? ($("#alert-error").text(t("密码")),
                    void $("#alert-error").show()) : ($(this).find("*").attr("disabled", !0),
                    r.parent().addClass("wait"),
                    void o(n, s, function() {
                        if ("undefined" != typeof analytics) {
                            var t = {
                                account_key: s.email,
                                source: location.href
                            };
                            analytics.track("register" === n ? "Signed Up" : "Signed In", t)
                        }
                    }, function(t) {
                        var e = "";
                        e = 400 === t.status ? "提交失败，请填写所有必须资料。" : 401 === t.status ? "登录失败，请检查注册邮箱和密码是否正确。" : 409 === t.status ? "注册失败，此邮箱已经注册。请使用其他邮箱注册或直接登录。如果问题依然存在，请联系 support@youdaxue.com" : "提交错误或服务器忙，请稍后重试。如果问题依然存在，请联系 support@youdaxue.com",
                            $("#alert-error").text(e),
                            $("#alert-error").show()
                    }, function(t) {
                        r.parent().removeClass("wait"),
                            r.find("*").attr("disabled", !1)
                    }))
            }),
            ["signin", "signup"].forEach(function(t) {
                $("#tab-" + t).on("click", function() {
                    var e = $(this).data("prefix")
                        , a = "/" + (e || "") + t + "/";
                    "" !== location.search && (a += location.search),
                    $.urlParam("action") && (a += "&action=" + $.urlParam("action")),
                        location.href = a
                })
            })
    })
}();
